<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    class Blog extends CI_Controller { 
        
        public function index() {
            $data['title'] = 'Blog Page of Techouse';
            $data['main_content'] = 'blog_view';
            $data['curr_page'] = 4;
            $this->load->model('posts');
            $this->load->model('categories');
            $data['rows'] = $this->posts->selectPosts();
            $data['rows_cat'] = $this->categories->getCategories();
            $this->load->view('template',$data);
        }
        
        function removePost($id) {
            $this->load->model('posts');
            $this->posts->deletePost($id);
            redirect($_SERVER['HTTP_REFERER']);
        }
        
        
        function editPost($id) {
            $data['main_content'] = 'editPost';
            $data['curr_page'] = 0;
            $this->load->model('posts');
            $data['row'] = $this->posts->editPost($id);
            $this->load->view('template',$data);
            
        }
        
        function editedPost($id) {
            $title = $this->input->post('title');
            $content = $this->input->post('content');
            
            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');
            
            if($this->form_validation->run() == FALSE) {
                $data['main_content'] = 'editPost';
                $data['curr_page'] = 0;
                $data['title'] = 'Admin area';
                $this->load->view('template', $data);
            }else {
                $query = $this->db->query("UPDATE posts SET title='".$title."', content='".$content."' WHERE id='".$id."'");
                redirect('blog');
            }
        }
        
        function addCategory() {
            $data['main_content'] = 'addCategory';
            $data['curr_page'] = 0;
            $data['title'] = 'Admin area';
            $this->load->view('template', $data);
        }
        
        function addedCategory() {
            $cat_name = $this->input->post('cat_name');
            $this->form_validation->set_rules('cat_name', "Category Name", 'required|min_length[3]');
            
            if($this->form_validation->run() == FALSE) {
                $data['main_content'] = 'addCategory';
                $data['curr_page'] = 0;
                $data['title'] = 'Admin area';
                $this->load->view('template', $data);
            }else {
                $query = $this->db->insert('categories', array(
                    'category_name' => $cat_name
                ));
                redirect('blog');
            }
        }
        
        function removeCategory($id) {
            $this->load->model('categories');
            $this->categories->removeCategory($id);
            redirect('blog');
        }
        
        
    }